package com.powerhua.builder;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 *
 * @author Administrator
 */
public class JavascriptHtmlBuilder {

    private final static String BASE_PATH = "D:/01 Jessica/My.Documents/My Dropbox/project/xsys/web/";
    //private final static String BASE_PATH = "D:/Documents/My Dropbox/project/xsys/web/";

    public static void main(String[] args) {
        JavascriptHtmlBuilder builder = new JavascriptHtmlBuilder();
        String[] transArr = {"oa/assistant/meeting/meeting_room_editor.html",
            "oa/assistant/meeting.js",
            "_editorHtml:", "    _editorHtml: '", "'"
        };

        String pageContent = builder.getPageContent(BASE_PATH + transArr[0]);
        String targetPageContent = builder.replaceTextFileStr(BASE_PATH + transArr[1],
            "UTF-8", transArr[2], transArr[3] + pageContent + transArr[4]);
        builder.writeFile(BASE_PATH + transArr[1], targetPageContent, "UTF-8");
    }

    private String getPageContent(String filePath) {
        StringBuffer result = new StringBuffer("");

        String content = getTextFileStr(filePath, "UTF-8");
        int start = content.indexOf("<body>");
        int end = content.indexOf("</body>");
        content = content.substring(start + 6, end);
        result.append(content);

        return result.toString();
    }

    private String getTextFileStr(String filePath, String coding) {
        StringBuffer sb = new StringBuffer();
        BufferedReader reader;
        try {
            reader = new BufferedReader(new InputStreamReader(
                new FileInputStream(filePath), coding));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                sb.append(tempStr.trim());
            }
            reader.close();
        } catch (Exception e) {
            // TODO 
            e.printStackTrace();
        }
        return sb.toString();
    }

    private String replaceTextFileStr(String filePath, String coding,
        String replaceStr, String targetStr) {
        StringBuffer sb = new StringBuffer();
        BufferedReader reader;
        try {
            reader = new BufferedReader(new InputStreamReader(
                new FileInputStream(filePath), coding));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                if (tempStr.indexOf(replaceStr) > 0) {
                    sb.append(targetStr);
                } else {
                    sb.append(tempStr);
                }
                sb.append(System.getProperty("line.separator"));
            }
            reader.close();
        } catch (Exception e) {
            // TODO 
            e.printStackTrace();
        }
        return sb.toString();
    }

    private void writeFile(String filePath, String content, String coding) {
        try {
            OutputStreamWriter out = new OutputStreamWriter(
                new FileOutputStream(filePath), coding);
            out.write(content);
            out.flush();
            out.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
